package com.octopus.yangj.customized.controller;

import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;

import com.octopus.kernal.controller.AbstractValidator;
import com.octopus.yangj.generated.model.EmployeeStruct;

@Component
public class CustomizedEmployeeValidator extends AbstractValidator{
	
	private static final String SIMPLE_EMAIL_REGEX = "[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}";
	
    @Override
    public void validate(Object o, Errors errors) {
    	EmployeeStruct entity = ((EmployeeStruct) o);
        if( StringUtils.isNotBlank( entity.getEmail() ) && !Pattern.matches( SIMPLE_EMAIL_REGEX, entity.getEmail().toUpperCase() ) ) {
            errors.rejectValue( "email", "error.common.field.invalid", 
            		new Object[]{messageSource.getMessage("label.user.field.email", new Object[]{}, LocaleContextHolder.getLocale())}, "Please enter a valid email address." );
        }
    }

}
